package com.pengheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pengheng.entity.SysOperLog;
import com.pengheng.model.ResultVo;
import com.pengheng.query.CommonQuery;
import com.pengheng.service.ISysOperLogService;
import com.pengheng.util.PageUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 操作日志记录 前端控制器
 *
 * @author pengheng
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/operLog")
public class SysOperLogController {

    @Resource
    private ISysOperLogService sysOperLogService;

    /**
     * 查询操作日志记录
     *
     * @param id
     * @return
     */
    @PostMapping("/get")
    public ResultVo<SysOperLog> get(Long id) {
        SysOperLog sysOperLog = sysOperLogService.getById(id);
        return ResultVo.result("获取数据成功", sysOperLog);
    }

    /**
     * 查询操作日志记录
     *
     * @param commonQuery
     * @return
     */
    @PostMapping("/list")
    public ResultVo<SysOperLog> list(@RequestBody CommonQuery commonQuery) {
        PageUtil.startPage(commonQuery);
        LambdaQueryWrapper<SysOperLog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByDesc(SysOperLog::getOperTime);
        List<SysOperLog> list = sysOperLogService.list(queryWrapper);
        return ResultVo.page(list);
    }

    /**
     * 插入/修改操作日志记录
     *
     * @param sysOperLog
     * @return
     */
    @PostMapping(value = "/save")
    public ResultVo<Void> save(@RequestBody SysOperLog sysOperLog) {
        boolean saveOrUpdate = sysOperLogService.saveOrUpdate(sysOperLog);
        return saveOrUpdate ? ResultVo.success("保存数据成功") : ResultVo.error("保存数据失败");
    }

    /**
     * 删除操作日志记录
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/delete")
    public ResultVo<Void> delete(Long id) {
        boolean removeById = sysOperLogService.removeById(id);
        return removeById ? ResultVo.success("删除数据成功") : ResultVo.error("删除数据失败");
    }
}
